package com.ry.flink.jobx;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamStatementSet;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

/**
 *  5. 把A表的数据写到B表
 * */
public class Job5 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        StreamTableEnvironment tablEnv = StreamTableEnvironment.create(env);

        //API
        tablEnv.from("inputTable").executeInsert("ouputTable");
        //SQL
        tablEnv.executeSql("INSERT INTO ouputTable SELECT * FROM inputTable");

        //输入写到多个输出
        StreamStatementSet statementSet = tablEnv.createStatementSet();
        statementSet.addInsert("outputTable1",tablEnv.from("inputTable"))
                .addInsert("outputTable2",tablEnv.from("inputTable"))
                .execute();

        //基于SQL
        tablEnv.createStatementSet()
                .addInsertSql("INSERT INTO ouputTable1 SELECT * FROM inputTable")
                .addInsertSql("INSERT INTO ouputTable2 SELECT * FROM inputTable")
                .execute();

//        env.execute("Job5");

        //table local sink
        tablEnv.from("inputTable").execute().print();
        tablEnv.executeSql("SELECT ...").print();
    }
}
